import requests
import time
import re
import json
from cipher import aes_cipher


JWXTXS = 'http://cas.hfut.edu.cn/cas/login?service=http://jxglstu.hfut.edu.cn/eams5-student/neusoft-sso/login'
TSGXT = 'http://cas.hfut.edu.cn/cas/login?service=http://210.45.242.5:8080/reader/hwthau2.php'
HQXT = 'http://cas.hfut.edu.cn/cas/login?service=http://hq.hfut.edu.cn/appdeskWeb/login?goto=/appdeskWeb/index'
XGGLPT = 'http://stu.hfut.edu.cn/xsfw/sys/emapfunauth/casValidate.do'

def login_one(username, password):
    session = requests.session()
    r = session.get(
        r'https://cas.hfut.edu.cn/cas/login?service=https%3A%2F%2Fcas.hfut.edu.cn%2Fcas%2Foauth2.0%2FcallbackAuthorize%3Fclient_name%3DCasOAuthClient%26client_id%3DBsHfutEduPortal%26redirect_uri%3Dhttps%253A%252F%252Fone.hfut.edu.cn%252F%26response_type%3Dcode')
    execution = re.findall('name="execution" value="(.*?)"', r.text)[0]
    session.get(
        f'https://cas.hfut.edu.cn/cas/checkInitVercode?_={int(time.time()*1000)}')
    key = session.cookies['LOGIN_FLAVORING']
    result = aes_cipher(key, password)
    data = {
        'username': username,
        'capcha': '',
        'execution': execution,
        '_eventId': 'submit',
        'password': result,
        'geolocation': '',
        'submit': '登录'
    }
    session.post(r'https://cas.hfut.edu.cn/cas/login?service=https%3A%2F%2Fcas.hfut.edu.cn%2Fcas%2Foauth2.0%2FcallbackAuthorize%3Fclient_name%3DCasOAuthClient%26client_id%3DBsHfutEduPortal%26redirect_uri%3Dhttps%253A%252F%252Fone.hfut.edu.cn%252Fhome%252Findex%26response_type%3Dcode', data=data)
    authorize = session.get(
        'https://cas.hfut.edu.cn/cas/oauth2.0/authorize?response_type=code&client_id=BsHfutEduPortal&redirect_uri=https://one.hfut.edu.cn/home/index')
    while not authorize.history:
        print("No history")
        authorize = session.get(
            'https://cas.hfut.edu.cn/cas/oauth2.0/authorize?response_type=code&client_id=BsHfutEduPortal&redirect_uri=https://one.hfut.edu.cn/home/index')
    code = re.findall('code=(.*)', authorize.history[0].headers['Location'])[0]
    data = json.loads(session.get(
        f'https://one.hfut.edu.cn/api/auth/oauth/getToken?type=portal&redirect=https:%2F%2Fone.hfut.edu.cn%2Fhome%2Findex%3Fcode%3D{code}&code={code}').text)
    token = data['data']['access_token']
    session.get(
        'https://one.hfut.edu.cn/cas/bosssoft/checkToken?token={token}')
    session.headers.update({'Authorization': f'Bearer {token}'})
    return session


def login_webvpn(username, password):
    session = requests.session()
    r = session.get(
        r'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/login?service=https%3A%2F%2Fwebvpn.hfut.edu.cn%2Flogin%3Fcas_login%3Dtrue')
    execution = re.findall('name="execution" value="(.*?)"', r.text)[0]
    session.get(
        f'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/checkInitVercode?vpn-12-o1-cas.hfut.edu.cn&_={int(time.time()*1000)}')
    data = session.get(
        f'https://webvpn.hfut.edu.cn/wengine-vpn/cookie?method=get&host=cas.hfut.edu.cn&scheme=http&path=/cas/login&vpn_timestamp={int(time.time()*1000)}').text
    key = re.findall('LOGIN_FLAVORING=(.+?);', data)[0]
    result = aes_cipher(key, password)
    checkUserIdenty = {
        'username': username,
        'password': result,
        '_': str(int(time.time()*1000))
    }
    session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/policy/checkUserIdenty', params=checkUserIdenty)
    data = {
        'username': username,
        'capcha': '',
        'execution': execution,
        '_eventId': 'submit',
        'password': result,
        'geolocation': '',
        'submit': '登录'
    }
    session.post(
        r'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/login?service=https%3A%2F%2Fwebvpn.hfut.edu.cn%2Flogin%3Fcas_login%3Dtrue', data=data)
    return session


def login(username, password):
    session = login_webvpn(username, password)
    session.get(r'https://webvpn.hfut.edu.cn/https/77726476706e69737468656265737421fff944d22f367d44300d8db9d6562d/api/auth/oauth/loginUrl?vpn-12-o2-one.hfut.edu.cn&type=portal&redirect=https:%2F%2Fone.hfut.edu.cn%2F')
    session.get('https://webvpn.hfut.edu.cn/https/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/oauth2.0/authorize?response_type=code&client_id=BsHfutEduPortal&redirect_uri=https://one.hfut.edu.cn/')
    authorize = session.get(
        'https://webvpn.hfut.edu.cn/https/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/oauth2.0/authorize?response_type=code&client_id=BsHfutEduPortal&redirect_uri=https://one.hfut.edu.cn/')
    code = re.findall('code=(.*)', authorize.history[0].headers['Location'])[0]
    data = json.loads(session.get(
        f'https://webvpn.hfut.edu.cn/https/77726476706e69737468656265737421fff944d22f367d44300d8db9d6562d/api/auth/oauth/getToken?vpn-12-o2-one.hfut.edu.cn&type=portal&redirect=https:%2F%2Fone.hfut.edu.cn%2F%3Fcode%3D{code}&code={code}').text)
    token = data['data']['access_token']
    session.post(
        f'https://webvpn.hfut.edu.cn/wengine-vpn/cookie?method=set&host=one.hfut.edu.cn&scheme=https&path=/&ck_data=TOKEN%3D{token}')
    session.get(
        f'https://webvpn.hfut.edu.cn/https/77726476706e69737468656265737421fff944d22f367d44300d8db9d6562d/cas/bosssoft/checkToken?vpn-12-o2-one.hfut.edu.cn&token={token}')
    session.headers.update({'Authorization': f'Bearer {token}'})
    return session
